package cn.tedu.loding.controller;

import cn.tedu.loding.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.loding.pojo.dto.SkuAddNewDTO;
import cn.tedu.loding.pojo.vo.PictureListItemVO;
import cn.tedu.loding.pojo.vo.SkuListItemVO;
import cn.tedu.loding.service.IPictureService;
import cn.tedu.loding.service.ISkuService;
import cn.tedu.loding.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "07.sku模块")
@Slf4j
@RestController
@Validated
@RequestMapping("/sku")
public class SkuController {

    @Autowired
    private ISkuService skuService;

    public SkuController() {
        log.debug("创建控制器对象：SpuController");
    }

    @ApiOperation("添加sku")
    @ApiOperationSupport(order = 1000)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid SkuAddNewDTO skuAddNewDTO){
        log.debug("开始处理【添加sku】请求,参数为{}",skuAddNewDTO);
        skuService.addNew(skuAddNewDTO);
        String message = "添加sku成功!";
        log.debug(message);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除sku")
    @ApiOperationSupport(order = 1001)
    @ApiImplicitParam(name = "id", value = "skuId", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除sku失败，尝试删除的sku的ID无效！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据id删除sku】的请求，参数：{}", id);
        skuService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询sku列表")
    @ApiOperationSupport(order = 1002)
    @GetMapping("")
    public JsonResult<List<SkuListItemVO>> list() {
        log.debug("开始处理【查询sku列表】的请求，无参数");
        List<SkuListItemVO> list = skuService.list();
        return JsonResult.ok(list);

    }

}
